#include "tasklistwidget.h"
#include "mainwindow.h"
#include "restoretargetdialog.h"

TaskListWidget::TaskListWidget(MainWindow *parent) :
    QWidget(parent)
{
    this->parentWindow = parent;

    centralLayout = new QVBoxLayout();
    this->setLayout(centralLayout);

    listWidget = new QListWidget();
    listWidget->setObjectName("taskList");
    listWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    listWidget->setFocusPolicy(Qt::NoFocus);
    listWidget->setStyle(new QCommonStyle);
    centralLayout->addWidget(listWidget);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    centralLayout->addLayout(buttonLayout);

    QPushButton *backupButton = new QPushButton(tr("Backup"));
    backupButton->setObjectName("genericButton");
    buttonLayout->addWidget(backupButton);

    QPushButton *restoreButton = new QPushButton(tr("Restore"));
    restoreButton->setObjectName("genericButton");
    buttonLayout->addWidget(restoreButton);

    QPushButton *editButton = new QPushButton(tr("Edit Task"));
    editButton->setObjectName("genericButton");
    buttonLayout->addWidget(editButton);

    //connects
    connect(listWidget, &QListWidget::currentItemChanged, this, &TaskListWidget::onSelectedTaskChanged);
    connect(backupButton, &QPushButton::clicked, this, &TaskListWidget::onBackupButtonClicked);
    connect(restoreButton, &QPushButton::clicked, this, &TaskListWidget::onRestoreButtonClicked);

    refreshTasks();
}

void TaskListWidget::refreshTasks()
{
    listWidget->clear();

    QList<quint32> tasks = DBEngine::loadTaskIDs();
    foreach(quint32 task_id, tasks)
    {
        QSharedPointer<BackupTask> task = QSharedPointer<BackupTask>(new BackupTask(task_id));

        TaskWidget *widget = new TaskWidget(task);
        connect(widget, &TaskWidget::removeTaskClicked, this, &TaskListWidget::onDeleteTaskClicked);
        connect(widget, &TaskWidget::removeInstanceClicked, this, &TaskListWidget::onDeleteInstanceClicked);
        QListWidgetItem * item = new QListWidgetItem();
        item->setSizeHint(widget->sizeHint());
        listWidget->addItem(item);
        listWidget->setItemWidget(item, widget);
    }
}

void TaskListWidget::onSelectedTaskChanged()
{

}

void TaskListWidget::onBackupButtonClicked()
{
    if(!listWidget->selectedItems().isEmpty()) {
        TaskWidget * widget = qobject_cast<TaskWidget *>(listWidget->itemWidget(listWidget->currentItem()));
        QSharedPointer<BackupTask> task = widget->getTask();

        emit taskBackupActivated(task);
    }
}

void TaskListWidget::onRestoreButtonClicked()
{
    if(!listWidget->selectedItems().isEmpty()) {
        TaskWidget * widget = qobject_cast<TaskWidget *>(listWidget->itemWidget(listWidget->currentItem()));
        QSharedPointer<BackupTask> task = widget->getTask();

        //If there is any selected instance inside widget, allow its restore
        if(widget->taskInstanceSelected())
        {
            if(task->getInstances().contains(widget->getSelectedInstance())) {
                QSharedPointer<RestoreTask> restoreTask =
                        QSharedPointer<RestoreTask>(new RestoreTask(*task,
                                                                    widget->getSelectedInstance(),
                                                                    RestoreTask::Original));

                RestoreTargetDialog dialog(restoreTask, this);
                if(dialog.exec() == QDialog::Accepted) {
                    emit taskRestoreActivated(qSharedPointerCast<BackupTask>(restoreTask));
                }
            }
        }
    }
}

void TaskListWidget::onEditButtonClicked()
{
    if(!listWidget->selectedItems().isEmpty()) {
        TaskWidget * widget = qobject_cast<TaskWidget *>(listWidget->itemWidget(listWidget->currentItem()));
        QSharedPointer<BackupTask> task = widget->getTask();
        //TODO Add task editing
    }
}

void TaskListWidget::onDeleteTaskClicked()
{
    TaskWidget * widget = qobject_cast<TaskWidget *>(QObject::sender());
    QSharedPointer<BackupTask> task = widget->getTask();

    QMessageBox msgBox;
    msgBox.setText("This action will remove all records about this task along with data.");
    msgBox.setInformativeText("Do you really want to proceed?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Yes);
    msgBox.setIcon(QMessageBox::Question);
    if(msgBox.exec() == QMessageBox::Yes) {
        task->deleteTask();
    }

    emit taskChanged();
    refreshTasks();
}

void TaskListWidget::onDeleteInstanceClicked(QDateTime timestamp)
{
    TaskWidget * widget = qobject_cast<TaskWidget *>(QObject::sender());
    QSharedPointer<BackupTask> task = widget->getTask();

    QMessageBox msgBox;
    if(task->instanceHasChildren(timestamp)) {
        msgBox.setText("Removal of this backup instance will cause removal of all dependent instances.");
    } else {
        msgBox.setText("This action will remove all records about backup instance along with data.");
    }
    msgBox.setInformativeText("Do you really want to proceed?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Yes);
    msgBox.setIcon(QMessageBox::Question);
    if(msgBox.exec() == QMessageBox::Yes) {
        task->deleteInstance(timestamp);
    }

    refreshTasks();
}
